import type {IComponent} from '@bastet/widgets';

export function createFunc(path: any[]): (draft: any, callback: (arg: IComponent[]) => IComponent[]) => void {
  let func = `draft`;

  while (path?.length) {
    const index = path.shift()
    if (index !== 'playground') {
      func += `[${ +index }].children`;
    }
  }

  // dart[index].children = callback(${func})
  func += ` = callback(${func})`

  return new Function('draft', 'callback', func) as (() => void)
}

export function createInformationFunc(path: string[]): (draft: any, callback: (arg: IComponent[]) => IComponent[]) => void {
  let func = `draft`;

  // draft
  // draft.children[2]
  // draft.children[2].children[1]
  while (path.length) {
    const index = path.shift()
    if (func === 'draft') {
      func += `[${ +index }]`;
    } else {
      func += `.children[${ +index }]`;
    }
  }

  func += ` = callback(${func})`

  console.log(func)
  return new Function('draft', 'callback', func) as (() => void)
}

export function createRemoveFunc(path: string[]) {
  let func = `draft`;
  const replace = `.splice(${path.pop()}, 1)`
  // .splice(index, 0, dataTransfer)
  while (path.length) {
    const index = path.shift()

    func += `[${ +index }].children`;
  }
  func += replace;
  return new Function('draft', func)
}
